package com.soft.util;

import java.util.Date;
import java.util.Stack;

import org.apache.commons.lang.StringUtils;

import com.soft.app.ruanba.down.Soft;

public class RuanXmlHelper extends org.xml.sax.helpers.DefaultHandler{
	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RuanXmlHelper.class);
	private java.util.Stack tags;
	private static int i = 1;
	private DatabaseHelper dbHelper;
	private Soft soft;
	private java.lang.StringBuilder sb;

	public RuanXmlHelper()
	{
		tags = new Stack();
		dbHelper = new DatabaseHelper();
		soft = null;
		sb = new StringBuilder();
	}

	public void startDocument()
		throws org.xml.sax.SAXException
	{
		log.info("开始解析");
	}

	public void characters(char ch[], int start, int length)
		throws org.xml.sax.SAXException
	{
		java.lang.String tag = (java.lang.String)tags.peek();
		java.lang.String value = new String(ch, start, length);
		if (tag.equals("name"))
			soft.setName(value);
		else
		if (tag.equals("description"))
			soft.setDescription(value);
		else
		if (tag.equals("image"))
		{
			if(StringUtils.isNotEmpty(value)){
				soft.setImage(value.replace("http://mz.ruan8.com", ""));
				soft.setImageFlag("1");
			}else{
				soft.setImageFlag("0");
			}
		} 
		else
		if (tag.equals("factory"))
			soft.setCompany(value.replace("开发厂商：", ""));
		else
		if (tag.equals("type"))
			soft.setFileFormat(value);
		else
		if (tag.equals("filename")){
			String[] a = value.split("/");
			if(a.length >= 3){
				soft.setFilename(a[2]);
			}else{
				soft.setFilename(value);
			}
			soft.setFilePath(value);
		}
		else
		if(tag.equals("size")){
			String s = value.replace("软件大小：", "").trim();
			soft.setSize(s);
		}
		else
		if (tag.equals("license"))
			soft.setLicense(value.replace("软件性质：", ""));
		else
		if (tag.equals("sys"))
			soft.setSystem(value);
		else
		if (tag.equals("model"))
			soft.setModel(value.replace("适用机型：", ""));
		else
		if (tag.equals("desc"))
			soft.setDescription(value);
		else
		if (tag.equals("language"))
			if(value.replace("软件语言：", "").equals("简体中文"))
				soft.setLanguageId(1);
			else if(value.replace("软件语言：", "").equals("英文"))
				soft.setLanguageId(2);
			else
				soft.setLanguageId(1);
		else
		if (tag.equals("signed"))
			if(value.equals("已签名"))
				soft.setSigned("2");
			else
				soft.setSigned("1");
		
	}

	public void endDocument()
		throws org.xml.sax.SAXException
	{
		log.info("成功结束任务!");
	}

	public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String name)
		throws org.xml.sax.SAXException
	{
		java.lang.String popName = (java.lang.String)tags.pop();
		if (name.equals("soft"))
			dbHelper.save(soft);
	}

	public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String name, org.xml.sax.Attributes attributes)
		throws org.xml.sax.SAXException
	{
		tags.push(name);
		if (name.equals("soft"))
		{
			soft = new Soft();
			//soft.setId(UuidFactory.getUuid());
			soft.setViewCount(0);
			soft.setDlCount(0);
			soft.setActived("1");
			soft.setCreated(new Date());
			soft.setUpdated(new Date());
			soft.setCreatedUserId("00000000000000000000000000000000");
			soft.setUpdatedUserId("00000000000000000000000000000000");
			soft.setDel("0");
		}
	}
}